package com.pwser.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/login")
public class LoginController {
	
	@RequestMapping(value="/welcome")
    public ModelAndView welcome() {
        System.out.println("访问成功！");
        ModelAndView mov=new ModelAndView();
        mov.setViewName("index");
        mov.addObject("msg", "测试成功!");
        return mov;
    }
	
	@RequestMapping(value="/ajaxTest", method=RequestMethod.POST)
	@ResponseBody
    public Map<String, String> ajaxTest(HttpServletRequest request) {
		String name = request.getParameter("name");
		System.out.println(name + "-------");
        Map<String, String> userMap = new HashMap<String, String>();
        userMap.put("id", "1001");
        userMap.put("loginName", "dlzx1");
        userMap.put("passWord", "123");
        userMap.put("userName", "大连坐席1");
        userMap.put("userRole", "系统管理员");
        userMap.put("userPurview", "抢修指挥,供电质量,业扩指挥");
        return userMap;
    }
	
	@RequestMapping(value="/doLogin", method=RequestMethod.POST)
	public String doLogin(HttpServletRequest request, Model model) {
		String msg = "";
		String loginAccount = request.getParameter("loginAccount");
		String loginPass = request.getParameter("loginPass");
		String rememberMe = request.getParameter("rememberMe");
		UsernamePasswordToken token = new UsernamePasswordToken(loginAccount, loginPass);
		if(rememberMe != null){
			token.setRememberMe(true);
		}
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			if (subject.isAuthenticated()) {
				msg = "登录用户：" + token.getPrincipal();
				model.addAttribute("msg", msg);
				return "index";
			}
		} catch (IncorrectCredentialsException e) {
			msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";
			model.addAttribute("msg", msg);
			System.out.println(msg);
		} catch (ExcessiveAttemptsException e) {
			msg = "登录失败次数过多";
			model.addAttribute("msg", msg);
			System.out.println(msg);
		} catch (LockedAccountException e) {
			msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";
			model.addAttribute("msg", msg);
			System.out.println(msg);
		} catch (DisabledAccountException e) {
			msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";
			model.addAttribute("msg", msg);
			System.out.println(msg);
		} catch (ExpiredCredentialsException e) {
			msg = "帐号已过期. the account for username " + token.getPrincipal() + "  was expired.";
			model.addAttribute("msg", msg);
			System.out.println(msg);
		} catch (UnknownAccountException e) {
			msg = "帐号不存在. There is no user with username of " + token.getPrincipal();
			model.addAttribute("msg", msg);
			System.out.println(msg);
		} catch (UnauthorizedException e) {
			msg = "您没有得到相应的授权！" + e.getMessage();
			model.addAttribute("msg", msg);
			System.out.println(msg);
		}
        return "jsp/login";
    }
	
	@RequiresRoles(value={"系统管理员", "查询用户"}, logical=Logical.OR)
	@RequestMapping(value="/permsTest")
	public String permsTest() {
		return "jsp/zhfw";
	}

}
